import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";

/**
 * 电量电能检测因子字典管理
 * guopeng
 * 2017.11.27
 */
export class ElecFactor{
  constructor(
    private id : string,//污染物id
    private elecFactorCode : string,//电量电能因子编码
    private elecFactorName : string,//电量电能因子名称
    private elecFactorUnit :string,//电量电能因子单位
    private elecFactorType : string,//电量电能因子类型
    private extensionInfo : string//拓展信息
  ){}
}

@Component({
  selector: 'app-elecFactor',
  templateUrl: './elecFactor.component.html',
  styleUrls: ['./elecFactor.component.css']
})
export class ElecFactorComponent implements OnInit{
  private elecFactorCode : string;//电量电能因子编码
  private elecFactorName : string;//电量电能因子名称
  errorMessage:string;//錯誤消息
  private list:Array<any>;

  public queryMessage:ElecFactor = new ElecFactor("","","","","","");
  public updateMessages:ElecFactor = new ElecFactor("","","","","","");
  public addMessages:ElecFactor =  new ElecFactor("","","","","","");
  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  public url:string = this.httpURLService.getUrl("communication");

  constructor(private http: HttpClient,private httpURLService: HttpURLService){
  }
  add(){
    this.addMessages =  new ElecFactor("","","","","","");
  }

  userMessage(event:ElecFactor){
    let query = new ElecFactor("","","","","","");
    let update = new ElecFactor("","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
  }

  listMessage(info:any){
    let elecFactorCode = info.elecFactorCode== undefined ? '' : info.elecFactorCode ;
    let elecFactorName = info.elecFactorName== undefined ? '' : info.elecFactorName;
    this.http.get(this.url+'/elecFactor/queryElecFactorListByPage?elecFactorCode='+elecFactorCode+'&elecFactorName='+elecFactorName).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/elecFactor/addElecFactor',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101119301'){
          this.errorMessage = "电量电能监测因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119302'){
          this.errorMessage = "电量电能监测因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119303'){
          this.errorMessage = "电量电能监测因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119304'){
          this.errorMessage = "电量电能监测因子名称已存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/elecFactor/deleteElecFactor/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101119305'){
        this.errorMessage = "该电量电能监测因子不存在！";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/elecFactor/updateElecFactor',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101119301'){
          this.errorMessage = "电量电能监测因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119302'){
          this.errorMessage = "电量电能监测因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119303'){
          this.errorMessage = "电量电能监测因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119304'){
          this.errorMessage = "电量电能监测因子名称已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119305'){
          this.errorMessage = "该电量电能监测因子不存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let elecFactorCode = this.elecFactorCode== undefined ? '' : this.elecFactorCode ;
    let elecFactorName = this.elecFactorName== undefined ? '' : this.elecFactorName;
    let current = pageNo;
    this.http.get(this.url+'/elecFactor/queryElecFactorListByPage?elecFactorCode='+elecFactorCode+'&elecFactorName='+elecFactorName+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/elecFactor/queryElecFactorListByPage').subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
	}
}

